package com.ld.control.business.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.control.business.customer.mapper.CustomerMapper;
import com.ld.control.business.customer.model.CustomerModel;
import com.ld.control.business.customer.service.CustomerService;
import com.ld.control.business.customer.vo.req.CustomerReqVo;
import com.ld.control.business.customer.vo.reqsp.CustomerRespVo;
import com.ld.control.common.vo.GeneratePageListVo;
import com.ld.control.exception.BusinessException;
import com.ld.control.utils.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2024-05-06
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, CustomerModel> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private JWTUtils jwtUtils;

    @Override
    public void add(CustomerReqVo reqVo) {
        addOrUpdateCheck(reqVo);
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        CustomerModel customerModel = new CustomerModel();
        BeanUtils.copyProperties(reqVo, customerModel);
        customerModel.setCreateBy(currentUserAccountId);
        customerModel.setCreateTime(new Date());
        customerMapper.insert(customerModel);

    }

    @Override
    public void update(CustomerReqVo reqVo) {
        addOrUpdateCheck(reqVo);
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        CustomerModel customerModel = new CustomerModel();
        BeanUtils.copyProperties(reqVo, customerModel);
        customerModel.setUpdateBy(currentUserAccountId);
        customerModel.setUpdateTime(new Date());
        customerMapper.updateById(customerModel);
    }

    @Override
    public CustomerRespVo getById(Long id) {
        CustomerRespVo customerRespVo = new CustomerRespVo();
        CustomerModel customerModel = customerMapper.selectById(id);
        BeanUtils.copyProperties(customerModel, customerRespVo);
        return customerRespVo;
    }

    @Override
    public void deleteById(Long id) {
        customerMapper.deleteById(id);
    }

    @Override
    public GeneratePageListVo<CustomerRespVo> getPageList(String name, String companyName, Integer current, Integer size) {
        IPage<CustomerRespVo> iPage = new Page<>(current, size);
        IPage<CustomerRespVo> iPage1 = customerMapper.selectPageList(iPage, name, companyName);
        return new GeneratePageListVo<>(iPage1);
    }


    public void addOrUpdateCheck(CustomerReqVo reqVo) {

        QueryWrapper<CustomerModel> queryWrapper = new QueryWrapper<>();
        if (reqVo.getId() != null) {
            queryWrapper.ne(CustomerModel.ID, reqVo.getId());
        }
        queryWrapper.eq(CustomerModel.NAME, reqVo.getName());
        Long count = customerMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BusinessException("客户名称已经存在！");
        }
    }
}
